//
//  CHAnnounceViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/2.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHAnnounceViewController.h"
#import "CHButton.h"
#import "CHScreenView.h"
#import "PJ_Model.h"
@interface CHAnnounceViewController ()<UITextViewDelegate,CHScreenViewDelegate>
{
    UITextField *titleField;
    UILabel *placeholderLabel;
    UITextView *contentTextView;
//    UIButton *allBtn;
    CHButton *screenBtn;
    CHButton *allBtn;
    UIButton *sendBtn;
    CHScreenView *screenView;
    UIButton *coverBtn;
   
    
}

@property (nonatomic ,strong) NSMutableArray *selectedArr;
@end

@implementation CHAnnounceViewController
-(NSMutableArray *)selectedArr{
    if (!_selectedArr) {
        _selectedArr = [NSMutableArray array];
    }
    return _selectedArr;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"发布通知";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupRightBarButtonItem];
    
    [self setupUI];
    
}


-(void)setupRightBarButtonItem{
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.bounds = CGRectMake(0, 0, 40 ,15);
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
}

-(void)setupUI{
    
    titleField = [[UITextField alloc] initWithFrame:CGRectMake(15, 15, UIScreenW - 30, 40)];
    titleField.placeholder = @"请输入通知标题";
    titleField.font = [UIFont systemFontOfSize:14];
    titleField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:titleField];
    
    contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleField.frame) + 15, UIScreenW - 30, 200)];
    contentTextView.delegate = self;
    contentTextView.layer.cornerRadius = 5;
    contentTextView.layer.masksToBounds = YES;
    contentTextView.layer.borderWidth = 0.5;
    contentTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    contentTextView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:contentTextView];
    
    placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, contentTextView.w - 10, 25)];
    placeholderLabel.font = [UIFont systemFontOfSize:14];
    placeholderLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:0.5f];
    placeholderLabel.text = @"请输入通知内容...";
    [contentTextView addSubview:placeholderLabel];
    
    allBtn = [[CHButton alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(contentTextView.frame) + 20, 200, 30)];
    allBtn.selected = YES;
    allBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [allBtn setImage:[UIImage imageNamed:@"radio_unchoosed"] forState:UIControlStateNormal];
    [allBtn setImage:[UIImage imageNamed:@"radio_choosed"] forState:UIControlStateSelected];
    [allBtn setTitle:@"发送至全部企业" forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor colorWithHexString:@"999999" alpha:1.0f] forState:UIControlStateNormal];
    [allBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:allBtn];
    
    screenBtn = [[CHButton alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(allBtn.frame) + 5, 200, 30)];
    screenBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [screenBtn setImage:[UIImage imageNamed:@"radio_unchoosed"] forState:UIControlStateNormal];
    [screenBtn setImage:[UIImage imageNamed:@"radio_choosed"] forState:UIControlStateSelected];
    [screenBtn setTitle:@"发送至筛选企业" forState:UIControlStateNormal];
    [screenBtn setTitleColor:[UIColor colorWithHexString:@"999999" alpha:1.0f] forState:UIControlStateNormal];
    [screenBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:screenBtn];
    
    
    //  发布
    sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, UIScreenH - 40 - 10 - 64, UIScreenW - 30, 40)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.layer.masksToBounds = YES;
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    sendBtn.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    [sendBtn setTitle:@"发布" forState:UIControlStateNormal];
    [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendBtn];
    
    
}

-(void)rightBtnClick{
    
    if (!screenView) {
        coverBtn = [[UIButton alloc] initWithFrame:self.view.bounds];
        coverBtn.backgroundColor = [UIColor blackColor];
        coverBtn.alpha = 0.0;
        [coverBtn addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:coverBtn];
        

        screenView = [[CHScreenView alloc] initWithFrame:CGRectMake(0, -280, UIScreenW, 280)];
        screenView.delegate = self;
        [self.view addSubview:screenView];
        [UIView animateWithDuration:0.3 animations:^{
            
            screenView.frame = CGRectMake(0, 0, UIScreenW, 280);
            coverBtn.alpha = 0.5;
            
        }];
    }
    
    
    
}


-(void)coverBtnClick{
    
    [self dismiss];
}

-(void)dismiss{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        screenView.frame = CGRectMake(0, -280, UIScreenW, 280);
        
    } completion:^(BOOL finished) {
        [screenView removeFromSuperview];
        [coverBtn removeFromSuperview];
        screenView = nil;
        coverBtn = nil;
    }];
    
    
}



-(void)sendBtnClick{
   
    if (titleField.text.length == 0 ) {
        [MBProgressHUD showError:@"请输入标题"];
    }else if (contentTextView.text.length == 0){
        [MBProgressHUD showError:@"请输入通知内容"];
    }else if (allBtn.selected ==YES) {
        
        [MBProgressHUD showMessage:@"发送中···"];
        [self sendNotice];
        
    }else{
        if (self.selectedArr.count == 0) {
            
            [MBProgressHUD showError:@"请选择筛选企业"];
        }else{
            
            [MBProgressHUD showMessage:@"发送中···"];
            [self send];
            
        }
    }
    
    
}

-(void)send{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"userflag"] = [Util getValue:@"userflag"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"title"] = titleField.text;
    params[@"content"] = contentTextView.text;
    params[@"recipient"] = [self.selectedArr componentsJoinedByString:@","];
//    NSLog(@"%@",params);
    [MBProgressHUD hideHUD];
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovnewsrelease" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
//        
        if ([result[@"code"] isEqualToString:@"1"]) {
            [MBProgressHUD showSuccess:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"sendSuccessMessage" object:nil];
            
        }else{
            [MBProgressHUD showError:@"发送失败"];
        }
        

        
        
    }];
}


-(void)sendNotice{
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"userflag"] = [Util getValue:@"userflag"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"title"] = titleField.text;
    params[@"content"] = contentTextView.text;
    params[@"recipient"] = [self retunStringByArr:[Globle getInstance].datas];
//    NSLog(@"%@",params);
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovnewsrelease" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
//    NSLog(@"发布结果%@",result);
        [MBProgressHUD hideHUD];
        if ([result[@"code"] isEqualToString:@"1"]) {
            [MBProgressHUD showSuccess:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"sendSuccessMessage" object:nil];
        
        }else{
            [MBProgressHUD showError:@"发送失败"];
        }
     
        
        
    }];
}

-(NSString *)retunStringByArr:(NSArray *)arr{
    NSMutableArray *tempArr = [NSMutableArray array];
    for (PJ_Model *model in arr) {
        [tempArr addObject:model.areaid];
    }
    return [tempArr componentsJoinedByString:@","];
    
}


-(void)btnClick:(UIButton *)btn{

    if (btn == allBtn) {
        allBtn.selected = YES;
        screenBtn.selected = NO;
        
    }else{
        
        screenBtn.selected = YES;
        allBtn.selected = NO;
        
    }
}

#pragma mark - CHScreenViewDelegate
-(void)didClickItemWithSelectedArr:(NSMutableArray *)selectedArr{
    self.selectedArr = selectedArr;
    
}


-(void)textViewDidChange:(UITextView *)textView{
    
    placeholderLabel.hidden = contentTextView.text.length >0 ? YES :NO;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
